//
//  PersonalVC.m
//  saler-ios
//
//  Created by 王亚陆 on 17/5/27.
//  Copyright © 2017年 sayimba. All rights reserved.
//

#import "PersonalVC.h"
#import "MineDetailVC.h"
#import "MineModel.h"
#import "MineCell.h"
#import "MyWalletViewController.h"
#import "MyBillVC.h"
#import <UIImageView+WebCache.h>
#import "LoginViewController.h"
@interface PersonalVC ()
<
UITableViewDelegate,
UITableViewDataSource,
MineDetailVCDelegate
>

{
    
    UIView * nav;
    NSMutableDictionary * dictDic;

}
@property (nonatomic, strong) NSArray *buttonArray;
@property (nonatomic, strong) NSArray *imageArray;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) MineModel *mineModel;;
@property (nonatomic, weak) UITableView *tableView;

@end

@implementation PersonalVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    nav = [self createNavView];
    [self createNavTitle:@"个人中心"];
    [self.view addSubview:nav];
    
    self.view.backgroundColor = GrayColor;
    dictDic = [[NSMutableDictionary alloc]init];
    
    [self setUpUI];
    
    [self getServerDatasIsReload:NO];
    
}

- (void)setUpUI {
    
    self.navigationController.navigationBarHidden = YES;
    
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
    
    ///添加scrollView
    [self.view addSubview:self.scrollView];
    
    
    UIButton *logoutButton = [UIButton buttonWithType:UIButtonTypeCustom];
    logoutButton.backgroundColor = WhiteColor;
    logoutButton.frame = CGRectMake(0, (135+43+43+20)*kHeight, screenWidth - 0, cellHeight);
    [logoutButton setTitle:@"退出登录" forState:UIControlStateNormal];
    //    logoutButton.layer.cornerRadius = 5;
    logoutButton.titleLabel.font = Font(13);
    [logoutButton addTarget:self action:@selector(logoutButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [logoutButton setTitleColor:littleBlackColor forState:UIControlStateNormal];
    
    
//    [self.scrollView addSubview:logoutButton];

    
    
    
}


-(void)logoutButtonAction{
    
    
    [WKProgressHUD showInView:self.view withText:@"" animated:YES];
    
    __weak typeof(self) weakSelf = self;
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [WKProgressHUD dismissInView:weakSelf.view animated:YES];
        
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        
        LoginViewController *loginVC = [[LoginViewController alloc]init];
        UINavigationController *loginNC = [[UINavigationController alloc]initWithRootViewController:loginVC];
        [weakSelf presentViewController:loginNC animated:YES completion:nil];
        
        [userDefaults setObject:nil forKey:@"login"];
        [userDefaults setObject:nil forKey:@"phone"];
        [userDefaults setObject:nil forKey:login_token];
        [userDefaults setObject:nil forKey:@"userType"];
        
    });

    
}



#pragma mark - 获取服务器数据
- (void)getServerDatasIsReload:(BOOL)isReload {
    
    __weak typeof(self) weakSelf = self;
    
    [Common requestWithUrlString:login_user_currentuser contentType:application_json finished:^(id responseObj) {
        
        if ([responseObj[@"success"] boolValue]) {
            
            NSDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:responseObj[@"data"]];
            
            dictDic = dict.mutableCopy;
            weakSelf.mineModel = [MineModel mj_objectWithKeyValues:dict];
            
            [_tableView reloadData];
            
//            [weakSelf getValueString:isReload];
            
            
        }
        else
        {
            [WKProgressHUD popMessage:responseObj[@"message"]
                               inView:weakSelf.view
                             duration:1.5
                             animated:YES];
            
        }
        
    } failed:^(NSString *errorMsg) {
        
        NSLog(@"%@",errorMsg);
    }];
    
}


//查询用户信息
- (void)checkUserOnline{
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    
    NSString *valueString = [userDefaults objectForKey:@"login"];
    
    if (valueString == nil)
    {
        UIButton *button = (UIButton *)[self.view viewWithTag:600];
        
        button.hidden = YES;
    }
    else
    {
        UIButton *button = (UIButton *)[self.view viewWithTag:600];
        
        button.hidden = NO;
        
        NSString  *string = [NSString stringWithFormat:@"%@user",login_user_currentuser];
        
        __weak typeof(self) weakSelf = self;
        
        [Common requestWithUrlString:string
                         contentType:application_json
                            finished:^(id responseObj) {
                                
                                if ([responseObj[@"success"] boolValue]) {
                                    

                                }
                                
                            } failed:^(NSString *errorMsg) {
                                NSLog(@"%@",errorMsg);
                            }];
    }
}

#pragma mark 赋值方法
- (void)getValueString:(BOOL)isReload{
    
    NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0];
    
    MineCell *cell = [self.tableView cellForRowAtIndexPath:index];
    
    self.mineModel.isReload = isReload;
    
    if (isReload == YES)
    {
        //////viewWillAppear
    }
    else
    {
        //////viewDidLoad
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        [userDefaults setObject:self.mineModel.phone forKey:@"phone"];
    }
    cell.mineModel = self.mineModel;
}




#pragma mark - 表单协议
- (CGFloat)tableView:(UITableView *)tableView
heightForFooterInSection:(NSInteger)section{
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView
heightForHeaderInSection:(NSInteger)section{
    return 0.001;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    } else {
        
        return self.buttonArray.count;
        
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    
    switch (indexPath.section)
    {
        case 0:
        {
            MineCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mineCell"];
            if (!cell)
            {
                cell = [[MineCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"mineCell"];
            }

            cell.backgroundView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"7"]];
            cell.nameLabel.text = [[Common shared] backString:dictDic[@"realName"]];
            cell.phoneLabel.text = [[Common shared] backString:dictDic[@"loginName"]];
            cell.accessoryView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"personal_arrow"]];
            [cell.ImageView sd_setImageWithURL:[NSURL URLWithString:dictDic[@"picUrl"]] placeholderImage:[UIImage imageNamed:@"image_1"]];

            return cell;
            
        }
            break;
        default:
        {
            
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"buttonCell"];
            if (!cell)
            {
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"buttonCell"];
            }
            
            cell.textLabel.text = self.buttonArray[indexPath.row];
            
            cell.accessoryView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"personal_arrow"]];
            
            cell.textLabel.font = Font(FontOfSize14);
            cell.textLabel.textColor = littleBlackColor;
            cell.imageView.image = [UIImage imageNamed:self.imageArray[indexPath.row]];
            return cell;
        }
            break;
    }
}

- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return  135 * kHeight;
    } else {
        return  cellHeight;
    }
    
}

- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    
    
        if (indexPath.section == 0) {
            MineDetailVC *detailVC = [[MineDetailVC alloc]init];
            detailVC.delegate = self;
            [Common isLogin:self andPush:detailVC];
            
        }
        //我的钱包
        if (indexPath.row == 1 &&indexPath.section == 1) {
//            MyWalletViewController * wallet = [[MyWalletViewController alloc]init];
//            [self.navigationController pushViewController:wallet animated:YES];
            [WKProgressHUD popMessage:@"此功能暂不开通,敬请关注" inView:self.view duration:1.5 animated:YES];

//            [Common isLogin:self andPush:wallet];
        }
    
    //我的账单
    if (indexPath.row == 0 &&indexPath.section == 1) {
//        MyBillVC * bill = [[MyBillVC alloc]init];
        
        [WKProgressHUD popMessage:@"此功能暂不开通,敬请关注" inView:self.view duration:1.5 animated:YES];

        
//        [self.navigationController pushViewController:bill animated:YES];
//        [Common isLogin:self andPush:wallet];
    }


}

- (UIView *)tableView:(UITableView *)tableView
viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]init];
    
    view.backgroundColor = GrayColor;
    
    return view;
}


#pragma mark - MineDetailVCDelegate协议
- (void)mineDetailSaveNewInfo{
    [self getServerDatasIsReload:NO];
}



- (UIScrollView *)scrollView {
    if (!_scrollView) {
        
        CGRect frame = CGRectMake(0, NaviStatusH, screenWidth, screenHeight);
        
        _scrollView = [[UIScrollView alloc]initWithFrame:frame];
        
        _scrollView.backgroundColor = GrayColor;
        
        _scrollView.contentSize = CGSizeMake(screenWidth, screenHeight + 1);
        
        
        frame = CGRectMake(0, 0, screenWidth, screenHeight - 64 - 55 * kHeight);
        
        UITableView * tableView = [[UITableView alloc]initWithFrame:frame];
        
        self.tableView = tableView;
        
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.tableFooterView = [[UIView alloc]init];
        tableView.backgroundColor = GrayColor;
        
        [self.scrollView addSubview:tableView];
        
    }
    return _scrollView;
}

- (NSArray *)buttonArray {
    if (!_buttonArray)
    {

        _buttonArray =  @[@"销售账单",
                          @"我的钱包"];
        
    }
    return _buttonArray;
}

- (NSArray *)imageArray {
    if (!_imageArray)
    {
        _imageArray = @[@"order",
                        @"mymoney"];
    }
    return _imageArray;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    RootViewController *rootVC = [RootViewController defaultsTabBar];
    [rootVC setTabBarHidden:NO];
    
    //修改信息后 重新请求数据
    [self getServerDatasIsReload:YES];
//    [self checkUserOnline];
    
}

@end
